package com.csist.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class Upload {
    /**
     * 文件上传
     * @param file 文件流
     * @param path 文件路径
     * @return "filed":上传失败,上传完成后的文件绝对地址:上传成功
     */
    public static String upload(MultipartFile file, String path){
        if (file.isEmpty()) {
            return "filed";
        }
        String FileName = file.getOriginalFilename();
        String suffixName = FileName.substring(FileName.lastIndexOf("."));

        FileName = UUID.randomUUID() + suffixName;
        File dest = new File(path  + FileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
            return "filed";
        }
        return dest.getAbsolutePath();
    }
}
